package com.lf.yapinmall.common.validate;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

public class IntListValueConstraintValidator implements ConstraintValidator<IntListValue, Integer> {

    Set<Integer> listValues = new HashSet<>();

    /**
     *
     * @param constraintAnnotation 注解信息
     */
    @Override
    public void initialize(IntListValue constraintAnnotation) {

        int[] values = constraintAnnotation.values();
        for (int value : values) {
            listValues.add(value);
        }
    }

    /**
     *
     * @param value 需要校验的值
     * @param context
     * @return
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return listValues.contains(value);
    }
}
